using UnityEngine;
using QFramework;
using System.Linq;
using QAssetBundle;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class KinfeAbilitys : ViewController,IController,ICanReadModels,IAbilitys
	{
        public void AtkStart()
        {

            Debug.Log("发射飞刀");
            var enemies = FindObjectsByType<EnemyFathers>
                   (FindObjectsInactive.Exclude,
               FindObjectsSortMode.None).OrderBy(enemy => Player.Instance.Distance2D(enemy)).
             Take((DataManager.Instance.simpleKnifeModel.AbilityItemCount.Value + DataManager.Instance.abilityUpModel.AbilitysItemCount.Value) * 5);

            int i = 0;
            foreach (var item in enemies)
            {
                if (i < 4)
                {
                    ///播放音频
                    //ActionKit.DelayFrame(11 * i, () =>
                    //{
                    //    AudioKit.PlaySound(Sfx.KnifeFire);
                    //}).StartGlobal();
                    //i++;
                }

                if (item!=null)
                {

                    //Debug.Log("有敌人");
                    Knifes.Instantiate().Position(this.Position()).Show().Self(self =>
                    {

                        //var selfChage = self;
                        var direction = item.NormalizedDirection2DFrom(Player.Instance);
                        self.enemyFathers = item;
                        self.SetEnemyFather(item);
                        self.SetBulletScale(DataManager.Instance.simpleKnifeModel.AbilityItemScale.Value);
                        self.SetBulletSpeed(DataManager.Instance.simpleKnifeModel.AbilityItemSpeed.Value+
                            DataManager.Instance.abilityUpModel.BulletSpeeds.Value);
                        self.BulletMoveDic(item.NormalizedDirection2DFrom(Player.Instance));
                        //self.transform.up = direction;

                        //selfChage.transform.Translate(item.NormalizedDirection2DFrom(Player.Instance) * DataManager.Instance.simpleKnifeModel.AbilityItemSpeed.Value);
                        //var AttackCount = 0;


                        //self.OnTriggerEnter2DEvent(collider =>
                        //{

                        //    var hurtbox = collider.GetComponent<HitHurtBox>();
                        //    if (hurtbox != null)
                        //    {
                        //        if (hurtbox.Owner.CompareTag("Enemy"))
                        //        {
                        //            var enemyfather = hurtbox.Owner.GetComponent<EnemyFathers>();

                        //            if (enemyfather is IEnemy ienemy)
                        //            {
                        //                DamageSystem.SetDamage(DataManager.Instance.simpleKnifeModel.AbilityDamage.Value, enemyfather);
                        //                //enemyfather.Hurt(simpleKnifeModel.SimpleKnifeDamage);

                        //                AttackCount++;
                        //                if (AttackCount >= DataManager.Instance.abilityUpModel.AbilitysItemCount.Value)
                        //                {
                        //                    selfChage.DestroyGameObjGracefully();
                        //                }

                        //            }

                        //            //hurtbox.Owon.GetComponent<IEnemy>().Hurt(simpleKnifeModel.SimpleKnifeDamage);

                        //        }
                        //    }

                        //}).UnRegisterWhenGameObjectDestroyed(self);

                        //ActionKit.OnUpdate.Register(() =>
                        //{
                        //    if (Player.Instance)
                        //    {
                        //        if (Player.Instance.Distance2D(selfChage) > 20)
                        //        {
                        //            self.DestroyGameObjGracefully();
                        //        }
                        //    }
                        //}).UnRegisterWhenGameObjectDestroyed(self);

                    });
                }
                else
                {
                    Debug.Log("没搜集到敌人");
                }
            }
        }

        public void AtkStop()
        {
           
        }

        public void AtkUpDate()
        {
          
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

        public void ReadModels()
        {
            this.RegisterEvent<RemoveBulletEvent>(e =>
            {

                Debug.Log("接收到换弹命令");
                ///发射一次飞刀
                AtkStart();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        void Start()
		{
            ReadModels();

        }


        
	}
}
